package com.xlh.controller;

import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.UserMessageDTO;
import com.xlh.param.SendNewMessageParam;
import com.xlh.param.UpdateMessageParam;
import com.xlh.pojo.UserMessage;
import com.xlh.pojo.user.Class;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.SendNewMessageService;
import com.xlh.service.user.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhr 2019/3/6
 */
@Api(tags = "SendNewMessage", description = "消息通知")
@RestController
@RequestMapping("/message")
public class SendNewMessageController {

    @Autowired
    private SendNewMessageService service;
    @Autowired
    private ClassService classService;
    @Autowired
    private UserInfoHolder userInfoHolder;

    @ApiOperation("新消息列表")
    @GetMapping("/new")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result<List<UserMessageDTO>> listNewMessage() {
        Long userId = userInfoHolder.getUserId();
        return Result.success(service.listNewMessage(userId));
    }

    @ApiOperation("学生消息列表")
    @GetMapping("/student")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result<PageDTO<UserMessageDTO>> studentMessage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "id", required = false) Long id) {
        Long userId = userInfoHolder.getUserId();
        return Result.success(service.listStudentMessage(pageNum, pageSize, id, userId));
    }

    @ApiOperation("删除学生消息")
    @DeleteMapping("/student/message/{id}")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result deleteStudentMessage(@PathVariable("id") Long id) {
        service.deleteStudentMessage(id);
        return Result.success();
    }

    @ApiOperation("变更学生消息阅读状态")
    @PutMapping("/student/message/{id}")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result changeReadStatus(@PathVariable("id") Long id) {
        service.changeReadStatus(id);
        return Result.success();
    }

    @ApiOperation("教师消息列表")
    @GetMapping("/teacher")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<PageDTO<UserMessage>> teacherMessage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Long userId = userInfoHolder.getUserId();
        return Result.success(service.listTeacherMessage(pageNum, pageSize, userId));
    }

    @ApiOperation("发送新消息班级列表")
    @GetMapping("/teacher/class-list")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<Class>> classList() {
        return Result.success(classService.getTeacherClass(userInfoHolder.getUserId(), false));
    }

    @ApiOperation("发送新消息")
    @PostMapping("/teacher/message")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result sendNewMessage(@RequestBody @Valid SendNewMessageParam param) {
        Long userId = userInfoHolder.getUserId();
        service.sendNewMessage(userId, param);
        return Result.success();
    }

    @ApiOperation("重新发送新消息")
    @PutMapping("/teacher/message/{id}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result resendMessage(@RequestBody @Valid UpdateMessageParam param, @PathVariable("id") Long id) {
        Long userId = userInfoHolder.getUserId();
        service.resendMessage(userId, id, param.getContent());
        return Result.success();
    }

    @ApiOperation("删除消息")
    @DeleteMapping("/teacher/message/{id}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result deleteMessage(@PathVariable("id") Long id) {
        Long userId = userInfoHolder.getUserId();
        service.deleteMessage(userId, id);
        return Result.success();
    }
}
